package org.springblade.tms.module.systemconfig;

import cn.hutool.core.collection.ListUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.tool.api.R;
import org.springblade.system.entity.DictBiz;
import org.springblade.system.feign.IDictBizClient;
import org.springblade.tms.global.exception.CommonException;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static org.springblade.tms.global.constant.DictCodeConstant.*;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.systemconfig
 * @className: SystemConfig
 * @author: yr
 * @description: 系统配置controller
 * @date: 2022/6/23 17:52
 */
@RestController
@AllArgsConstructor
@RequestMapping("systemConfig")
@Api(value = "系统配置-yr", tags = "系统配置-yr")
@GlobalTransactional
public class SystemConfigController {
	private final IDictBizClient dictBizClient;

	/**
	 * @return R<IPage < TMUserInfoVO>>
	 * @author yr
	 * @description 系统配置查询
	 * @date 2022/3/11
	 */
	@ApiLog("系统配置查询")
	@GetMapping("/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "系统配置查询")
	public R<List<DictBiz>> list() {
		return dictBizClient.getListByCode(SYSTEM_CONFIG);
	}


	/**
	 * @return R<IPage < TMUserInfoVO>>
	 * @author yr
	 * @description 系统配置查询
	 * @date 2022/3/11
	 */
	@ApiLog("系统配置单个查询")
	@GetMapping("/waybillSupportsSplitting")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "运单是否支持拆单")
	public R<String> waybillSupportsSplitting() {
		return dictBizClient.getValue(SYSTEM_CONFIG, WAYBILL_SUPPORTS_SPLITTING);
	}

	/**
	 * @return R<IPage < TMUserInfoVO>>
	 * @author yr
	 * @description 系统配置查询
	 * @date 2022/3/11
	 */
	@ApiLog("询价单分配承运商最大数")
	@GetMapping("/maximumNumberOfCarriers")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "询价单分配承运商最大数")
	public R<String> maximumNumberOfCarriers() {
		return dictBizClient.getValue(SYSTEM_CONFIG, MAXIMUM_NUMBER_OF_CARRIERS);
	}

	/**
	 * @return R<IPage < TMUserInfoVO>>
	 * @author yr
	 * @description 系统配置查询
	 * @date 2022/3/11
	 */
	@ApiLog("外贸询价单最大船公司数")
	@GetMapping("/maximumNumberOfShipping")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "外贸询价单最大船公司数")
	public R<String> maximumNumberOfShipping() {
		return dictBizClient.getValue(SYSTEM_CONFIG, MAXIMUM_NUMBER_OF_SHIPPING);
	}

	/**
	 * @return R<IPage < TMUserInfoVO>>
	 * @author yr
	 * @description 系统配置查询
	 * @date 2022/3/11
	 */
	@ApiLog("承运商组别")
	@GetMapping("/carrierGroup")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "承运商组别")
	public R<String> carrierGroup() {
		R<String> value = dictBizClient.getValue(SYSTEM_CONFIG, CARRIER_GROUP);
		if (value.getCode() != 200) {
			CommonException.throwCommonException("系统配置获取失败");
		}
		String data = value.getData();
		return R.data(data);
	}

	/**
	 * /**
	 *
	 * @param dictBiz:
	 * @author yr
	 * @description 修改系统配置
	 * @date 2022/3/10 15:44
	 */
	@ApiLog("修改系统配置")
	@PostMapping("/saveOrUpdate")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "修改系统配置", notes = "传入dictBiz")
	public R saveOrUpdate(@Valid @RequestBody List<DictBiz> dictBiz) {
		dictBiz.forEach(dictBiz1 -> {
			R<Boolean> submit = dictBizClient.submit(dictBiz1);
			if (!submit.getData()) {
				CommonException.throwCommonException(submit.getMsg());
			}
		});
		return R.status(true);
	}
}
